﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IRRemoteControl
{
    public class SmartHelicopterEncoder
    {
        List<double> pwlDict = new List<double>();

        public const int WaveSampleRate = 44100;
        public const double  BackgroundWaveCycle = 0.00016;

        public SmartHelicopterEncoder(params double[] pwlDict)
        {
            this.pwlDict.AddRange(pwlDict);
        }

        public void SendCommand(params byte[] pwlIndexes)
        {
            List<double> pwls = new List<double>();

            foreach (var pwlIndex in pwlIndexes)
            {
                pwls.Add(pwlDict[pwlIndex]);
            }

            SendCommand(pwls.ToArray());
        }

        public void SendCommand(params double[] pwls)
        {
            var audioData = new List<short>();

            var anglePerSample = 2 * Math.PI / (WaveSampleRate * BackgroundWaveCycle);
            var angle = 0.0;

            for (int iPwl = 0; iPwl < pwls.Length; iPwl++)
            {
                double pwl = pwls[iPwl];

                int carrierWaveSampleCount = (int)(pwl * WaveSampleRate);

                bool waveHigh = true;
                for (int iCarrierWave = 0; iCarrierWave < carrierWaveSampleCount; iCarrierWave++)
                {
                    angle += anglePerSample;

                    audioData.Add((short)(Math.Sin(angle) * short.MaxValue * 1.6));
                    
                    short sample = 0;

                    if (iPwl % 2 == 0)
                    {
                        sample = waveHigh ? short.MaxValue : short.MinValue;
                    }
                    
                    audioData.Add(sample);
                }
            }

            //var buffer = new AudioBuffer
            //{
            //    Stream = DataStream.Create(audioData.ToArray(), true, true),
            //    AudioBytes = audioData.Count * 2,
            //    Flags = BufferFlags.EndOfStream
            //};

            //XAudio2 xaudio = new XAudio2();
            //MasteringVoice masteringVoice = new MasteringVoice(xaudio);
            //var voice = new SourceVoice(xaudio, new WaveFormat(), true);

            //voice.SubmitSourceBuffer(buffer, null);

            //voice.Start();
        }

        // Data
        //2, 3,
        //        0, 0, 1, 0,
        //        1, 1, 1, 1,
        //        1, 0, 0, 1,
        //        1, 0, 1, 1,
        //        1, 0, 1, 1,
        //        1, 0, 0, 1,
        //        1, 1, 1, 0,
        //        0, 1, 0, 1,
        //        1, 1, 0, 1,
        //        1, 0, 1, 1,
        //        1, 0, 1, 1,
        //        1, 1, 0, 1,
        //        4, 5,
        //1,1,0,1,0,0,1,0,
        //1,0,0,1,0,0,0,0,
        //1,0,1,0,1,1,1,0,
        //1,1,1,0,1,1,1,0,
        //1,1,0,1,0,0,0,0,
        //0,0,1,0,0,0,0,0
    }
}
